﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.Enterprise;
using UtilReport;
using System.Collections;
using CrystalDecisions.Shared;
namespace UtilReportOMS
{
    public partial class UCTK : UserControl
    {
 #region Member

        CrystalDecisions.CrystalReports.Engine.TextObject txtObject; // đối tượng trình ký
        CrystalDecisions.CrystalReports.Engine.ReportDocument rptdoc; // trang báo cáo
        CrystalDecisions.CrystalReports.Engine.FieldObject mv_oRptFieldObj;
        public const string Name_trinh_ky = "Field150181";
        public List<clsThongTinTrinhKy> L_TT_TK= new List<clsThongTinTrinhKy>();
        public string strHeader = "";// tiêu đề báo cáo
        public string strMaDVQl = "";// thông tin mã dơn vị quản lý
        public string[] Feild_Names;
        public string[] Formula_Names;
        public bool Exists = false;
        public static bool isConnectSv = false;
        public static string CONNECTSTRING=@"/serTK/serTK.asmx";
 #endregion Member

 #region Construction

        public UCTK(string H, CrystalDecisions.CrystalReports.Engine.ReportDocument rdoc, string _strMaDVQl, string[] _Feild_Names, string[] _Formula_Names)
        {
            InitializeComponent();           
            rptdoc = rdoc;          
            strHeader = H;
            strMaDVQl = _strMaDVQl;
            Formula_Names = _Formula_Names;
            Feild_Names = _Feild_Names;              
        }

        public UCTK(string H, CrystalDecisions.CrystalReports.Engine.ReportDocument rdoc, string _strMaDVQl)
        {
            InitializeComponent();        
            rptdoc = rdoc;
            strHeader = H;
            strMaDVQl = _strMaDVQl;
            Formula_Names = new string[] { "Formula_1" };
            Feild_Names = new string[] { "Field150181" };        
        }    

#endregion Construction
        
 #region MyEvent

        private void Trinhky()
        {
            try
            {
                clsThongTinTrinhKy cls = new clsThongTinTrinhKy();
                if (!Exists)
                {
                    L_TT_TK = cls.getTrinhKyOnRpt(rptdoc.ToString(), strMaDVQl, rptdoc, Feild_Names, Formula_Names);
                }
                if (L_TT_TK.Count == 0)
                {
                    MessageBox.Show("Không có trình ký trong báo cáo này", "Thong bao");
                    return;
                }
                FormTrinhKy frm = new FormTrinhKy(L_TT_TK, Exists);
                frm.ShowDialog();
                cls.setChangeInRPT(L_TT_TK, rptdoc);
                SetInforInReport();

            }
            catch (Exception ex)
            {
                MessageBox.Show("Không có trình ký trong báo cáo này", "Thong bao");
                if (onError != null)
                {
                    onError(ex);
                }
            }
        }

        private void BtnClick(object sender, EventArgs e)
        {
            Button bt = (Button)sender;
            if (bt.Name == btnTrinhky.Name)
            {
                Trinhky();
            }
            else if (bt.Name == btnSizeDown.Name)
            {
                DecSize();
            }
            else if (bt.Name == btnSizeUp.Name)
            {
                IncSize();
            }
            else if (bt.Name == btnCanLe.Name)
            {
                SetBoderReport();
            }
        }       

        public void UserControl1_Load()
        {
                        
            try
            {
                this.CrtView.ReportSource = rptdoc;
               // mv_oRptFieldObj = (CrystalDecisions.CrystalReports.Engine.FieldObject)rptdoc.ReportDefinition.ReportObjects[Name_trinh_ky];
                //lấy thông tin trình ký trong DB
                clsThongTinTrinhKy cls = new clsThongTinTrinhKy();
                L_TT_TK = cls.getTrinhKyOnDb(rptdoc.ToString(), strMaDVQl);
                if (L_TT_TK.Count<=0)
                {
                    Exists = false;
                }
                else
                {
                    Exists = true;
                    //nếu có trong DB thì set luôn giá trị
                    cls.setChangeInRPT(L_TT_TK, rptdoc);                    
                    SetInforInReport();
                }
                isConnectSv = true;                
            }
            catch (Exception ex)
            {
                isConnectSv = false;
                if (onError != null)
                {
                    onError(ex);
                }                
            }
        }
        
  #endregion MyEvent

 #region MyMethod
        public void SetInforInReport()
        {            
                this.CrtView.ReportSource = rptdoc;            
        }

        public void DecSize()
        {
            CrystalDecisions.CrystalReports.Engine.FormulaFieldDefinition mFormular;
            //CrystalDecisions.CrystalReports.Engine.Section mSec;
            string mStr;
            try
            {
                foreach (CrystalDecisions.CrystalReports.Engine.Section mSec in rptdoc.ReportDefinition.Sections)
                {

                    mSec.Height = mSec.Height - 20;
                }
                //'rpt.Refresh()

                //'BRptBaoCao.RefreshReport()
               // SetParamAgain(rptdoc.ParameterFieldInfo);
                //rptdoc.Refresh();
                CrtView.ReportSource = rptdoc;
               // CrtView.RefreshReport();
               // CrtView.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thể giảm khoảng cách được nữa!", "Báo cáo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        public void IncSize()
        {
            CrystalDecisions.CrystalReports.Engine.FormulaFieldDefinition mFormular;
            //CrystalDecisions.CrystalReports.Engine.Section mSec;
            string mStr;
            try
            {
                foreach (CrystalDecisions.CrystalReports.Engine.Section mSec in rptdoc.ReportDefinition.Sections)
                {

                    mSec.Height = mSec.Height + 20;
                }
                //'rpt.Refresh()

                //getCurrentParameter();
        
                //'BRptBaoCao.RefreshReport()
              //  SetParamAgain(rptdoc.ParameterFieldInfo);
               // 
               //string v = ((ParameterDiscreteValue)rptdoc.ParameterFields[0].CurrentValues[0]).Value.ToString();
                CrtView.ReportSource = rptdoc;                
                //CrtView.RefreshReport();
                //rptdoc.Refresh();
                //CrtView.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thể tăng khoảng cách được nữa!", "Báo cáo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        public void SetParamAgain(CrystalDecisions.Shared.ParameterFields p)
        {
            try
            {
                CrystalDecisions.Shared.ParameterFields p0 = new ParameterFields();

                for (int i = 0; i < p.Count; ++i)
                {

                    rptdoc.SetParameterValue(p[i].ParameterFieldName, ((CrystalDecisions.Shared.ParameterDiscreteValue)p[i].CurrentValues[0]).Value);
                    
                    //'If p.Item(i).ParameterFieldName.ToUpper = "TXTNGUOIKY1" Then

                    //'RptDoc.SetParameterValue(p.Item(i).ParameterFieldName, mv_oNguoiKy.mv_NOI_DUNG)

                    //'End If

                }
            }
            catch { }
        }

        public void getCurrentParameter()
        {
            foreach (ParameterField Param in rptdoc.ParameterFields)
            {
                _CurrentParams.Add(Param.Name.ToUpper(), ((ParameterDiscreteValue)Param.CurrentValues[0]).Value.ToString());
            }
        }

        Hashtable _CurrentParams= new Hashtable();
        private void SetParameters(Hashtable Params)
        {
            rptdoc.DataDefinition.ParameterFields.Reset();
            foreach (ParameterField Param in rptdoc.ParameterFields)
            {
                if (Params.ContainsKey(Param.Name.ToUpper()))
                {
                    rptdoc.SetParameterValue(Param.Name, Params[Param.Name.ToUpper()]);
                }
                else
                {
                    rptdoc.SetParameterValue(Param.Name, Param.DefaultValues);
                }
            }
            //SetReport(rep);
        }


        public void SetBoderReport()
        {
            try
            {
                //lấy về lề của báo cáo.
                CanLe _canle = new CanLe();
                _canle.getInforToReport(rptdoc.PrintOptions.PageMargins);
                frmCanhLe frm = new frmCanhLe(_canle);
                frm.ShowDialog(this);
                if (frm.DialogResult == DialogResult.Yes)
                {
                    CrystalDecisions.Shared.PageMargins _pMargins = new CrystalDecisions.Shared.PageMargins();
                    _canle.setInforToReport(ref _pMargins);
                    rptdoc.PrintOptions.ApplyPageMargins(_pMargins);
                    this.CrtView.ReportSource = rptdoc;
                }
                frm.Dispose();
            }
            catch(Exception ex)
            {
                MessageBox.Show("Thông tin lề không phù hợp");
            }
        }

 #endregion

        public event OnExit Exit;
        public event OnError onError;
        public event OnSuccess onSuccess;
        public event ReportDoubleClick onReportDoubleClick;
        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (Exit != null)
            {
                Exit(this);
            }
        }

        private void CrtView_DoubleClick(object sender, EventArgs e)
        {
            if (onReportDoubleClick != null)
            {
                onReportDoubleClick(sender, e);
            }
        }

        private void CrtView_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.ToString() == "ESC")
            {
                if (Exit != null)
                {
                    Exit(this);
                }
            }
        }

        private void UCTK_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.ToString() == "ESC")
            {
                if (Exit != null)
                {
                    Exit(this);
                }
            }
        }
     
    }
    public delegate void ReportDoubleClick(object sender,EventArgs e);
    public delegate void OnExit(object sender);
    public delegate void OnError(Exception ex);
    public delegate void OnSuccess(String str);
}
